package com.ficv.tucanalmovil.dominio;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findContactosesByBlockedNot" })
public class Contactos {

    @OneToOne
    private People contactoA;

    @OneToOne
    private People contactoB;

    private long lastMsgId;

    private Boolean aToB;

    private Boolean bToA;

    private Integer blockedBtoA;
    
    private Integer blockedAtoB;  
    
    private String aAlias;
    
    private String bAlias;
    

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date hora;

    public static TypedQuery<com.ficv.tucanalmovil.dominio.Contactos> findContactos(People destinatario, Integer rel) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<Contactos> q = em.createQuery("SELECT o FROM Contactos AS o WHERE o.contactoA = :destinatario AND o.blockedAtoB < :rel OR o.contactoB = :destinatario AND o.blockedBtoA < :rel  ORDER BY o.lastMsgId DESC", Contactos.class);
        q.setParameter("destinatario", destinatario);
        q.setParameter("rel", rel);        
//        q.setParameter("blocked", new Boolean(true));
        return q;
    }

    public static TypedQuery<com.ficv.tucanalmovil.dominio.Contactos> findContactos(People remite, People destinatario) {
      

        return findContactos( remite,  destinatario,  2);
    }	
   
    
    public static TypedQuery<com.ficv.tucanalmovil.dominio.Contactos> findContactos(People remite, People destinatario, Integer rel) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<Contactos> q = em.createQuery("SELECT o FROM Contactos AS o WHERE   o.contactoA = :remite  AND o.contactoB = :destinatario AND o.blockedBtoA < :rel OR o.contactoA = :destinatario  AND o.contactoB = :remite AND o.blockedAtoB < :rel", Contactos.class);
        q.setParameter("destinatario", destinatario);
        q.setParameter("remite", remite);   
        q.setParameter("rel", rel);           

        return q;
    }    
    
    public static TypedQuery<com.ficv.tucanalmovil.dominio.Contactos> findNewMsgs(People destinatario) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<Contactos> q = em.createQuery("SELECT o FROM Contactos AS o WHERE   o.contactoA = :destinatario AND o.bToA=1 AND o.blockedAtoB < 1  OR  o.contactoB = :destinatario AND o.aToB=1 AND  o.blockedBtoA < 1  ORDER BY o.lastMsgId DESC", Contactos.class);
        q.setParameter("destinatario", destinatario);
//        q.setParameter("blocked", new Boolean(true));
        return q;
    }
    
    public static TypedQuery<com.ficv.tucanalmovil.dominio.People> findNewContacts(People me, List<String> tlfns) {
        if (me == null) throw new IllegalArgumentException("The me argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<People> q = em.createQuery("SELECT o FROM People AS o WHERE " +
        			"NOT EXISTS (SELECT c FROM Contactos AS c WHERE  c.contactoA=o AND c.contactoB=:me  OR c.contactoB=o AND c.contactoA=:me) " +
        			"AND  o.telefono IN :tlfns ", People.class);
        q.setParameter("me", me);
        q.setParameter("tlfns", tlfns);        
//        q.setParameter("blocked", new Boolean(true));
        return q;
    }    
}
